package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResMyBuyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyProductEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.AccountBankEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.RebateListEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class RebateListAdp extends RecyclerView.Adapter<RebateListAdp.ReabteListHolder>{

    private List<ResMyBuyProductEntity.DataBean.ListBean> mData = new ArrayList<>();

    private Context mContext;

    private LayoutInflater inflater;

    public RebateListAdp(Context mContext){
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    public void setData(List<ResMyBuyProductEntity.DataBean.ListBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResMyBuyProductEntity.DataBean.ListBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public ReabteListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_rebate_list,parent,false);
        return new ReabteListHolder(view);
    }

    @Override
    public void onBindViewHolder(ReabteListHolder holder, int position) {
        final ResMyBuyProductEntity.DataBean.ListBean entity = mData.get(position);
        holder.upadte(entity);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class ReabteListHolder extends RecyclerView.ViewHolder{

        //产品名称
        private TextView mNameTv;

        //金额
        private TextView mPriceTv;

        //结算方式
        private TextView mBalanceTv;

        //日收益率
        private TextView mIncomeTv;

        //日返利
        private TextView mRebateTv;

        //购买时间
        private TextView mTimeTv;

        public ReabteListHolder(View itemView) {
            super(itemView);
            mNameTv = itemView.findViewById(R.id.item_rebate_list_title_tv);
            mPriceTv = itemView.findViewById(R.id.item_rebate_list_price_tv);
            mBalanceTv = itemView.findViewById(R.id.item_rebate_list_balance_tv);
            mIncomeTv = itemView.findViewById(R.id.item_rebate_list_rebate_radio_tv);
            mRebateTv = itemView.findViewById(R.id.item_rebate_list_rebate_tv);
            mTimeTv = itemView.findViewById(R.id.item_rebate_list_time_tv);
        }

        public void upadte(ResMyBuyProductEntity.DataBean.ListBean entity){
            mNameTv.setText(entity.getProduct_name());
            mPriceTv.setText(entity.getPaid_fee());
            if("day_income".equals(entity.getIncome_type())){
                mBalanceTv.setText("日结");
            }else if("week_income".equals(entity.getIncome_type())){
                mBalanceTv.setText("周结");
            }else if("month_income".equals(entity.getIncome_type())){
                mBalanceTv.setText("月结");
            }
            if(!TextUtils.isEmpty(entity.getPer())) {
                if("null".equals(entity.getPer())) {
                    mIncomeTv.setText("0%");
                }else{
                    mIncomeTv.setText(entity.getPer() + "%");
                }
            }else{
                mIncomeTv.setText("0%");
            }
            mRebateTv.setText(entity.getIncome());
            if(!TextUtils.isEmpty(entity.getCreate_date())){
                mTimeTv.setText(TimeUtils.timeFomartOnlyDate(Long.valueOf(entity.getCreate_date())));
            }
        }
    }

    private RebateListListener listener;

    public void setRebateListListener(RebateListListener listener){
        this.listener = listener;
    }

    public interface RebateListListener{
        void onItemClick(ResMyBuyProductEntity.DataBean.ListBean entity);
    }
}
